package MyAdmin.Controller;

import MyFramePro.Domain.Entity.LoginUser;
import MyFramePro.Domain.Entity.Menu;
import MyFramePro.Domain.Entity.User;
import MyFramePro.Domain.ResponseResult;
import MyFramePro.Domain.Vo.BehindUserInfoVo;
import MyFramePro.Domain.Vo.RouterVo;
import MyFramePro.Domain.Vo.UserInfoVo;
import MyFramePro.Enums.AppHttpCodeEnum;
import MyFramePro.Exception.SystemException;
import MyFramePro.Mapper.UserMapper;
import MyFramePro.Service.Impl.BehindLoginServiceImpl;
import MyFramePro.Service.Impl.MenuServiceImpl;
import MyFramePro.Service.Impl.RoleServiceImpl;
import MyFramePro.Utils.BeanCopyUtils;
import MyFramePro.Utils.SecurityUtils;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
public class LoginController {

    @Resource
    private BehindLoginServiceImpl behindLoginService;

    @Resource
    private MenuServiceImpl menuService;


    @PostMapping(value = "/user/login")
    public ResponseResult login(@RequestBody User user){

        if(!StringUtils.hasText(user.getUserName())) {
            throw  new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);   //必须要传入用户名
        }

        return behindLoginService.login(user);
    }

    /**
     * 这里是根据对应的权限与角色，显示对应的功能信息
     */
    @GetMapping(value = "/getInfo")
    public ResponseResult getInfo(){

        return behindLoginService.getInfo();

    }


    /**
     * 这里是获取对应用户角色的相应显示的菜单信息
     */
    @GetMapping(value = "/getRouters")
    public ResponseResult  getRouters(){
            Long userId = SecurityUtils.getUserId();
            //查询menu 结果是tree的形式
            List<Menu> menus = menuService.selectRouterMenuTreeByUserId(userId);
            //封装数据返回
            return ResponseResult.okResult(new RouterVo(menus));
    }


    /**
     * 这里是退出登录的功能
     */
    @PostMapping(value = "/user/logout")
    public ResponseResult logout(){

        return behindLoginService.logout();
    }

}
